<template>
  <div class="container">
    <div class="header">
      <el-input
        v-model="searchKeyword"
        placeholder="请输入菜品名称"
        @keyup.enter.native="init"
        clearable
      >
        <i
          @click="init"
          slot="prefix"
          class="el-input__icon el-icon-search"
        ></i>
      </el-input>
      <el-button type="primary" @click="openAddForm">添加菜品</el-button>
      <el-button type="danger" @click="handleBatchDelete">批量删除</el-button>
      <el-button type="success" @click="handleBatchEnable">批量启售</el-button>
      <el-button type="warning" @click="handleBatchDisable">批量停售</el-button>
    </div>

    <el-table
      :data="tableData"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="name" label="菜品名称"></el-table-column>
      <el-table-column prop="price" label="价格"></el-table-column>
      <el-table-column prop="status" label="状态">
        <template v-slot="{ row }">
          {{ row.status ? "启售" : "停售" }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-link type="primary" @click="openEditForm(row)">编辑</el-link>
          <el-divider direction="vertical"></el-divider>
          <el-popconfirm
            title="确认删除该菜品?"
            @onConfirm="handleDelete(row)"
          >
            <el-link type="danger" slot="reference">删除</el-link>
          </el-popconfirm>
          <el-divider direction="vertical"></el-divider>
          <el-popconfirm
            title="确认调整该菜品的状态?"
            @onConfirm="handleChangeStatus(row)"
          >
            <el-link type="warning" slot="reference">{{
              row.status ? "停售" : "启售"
            }}</el-link>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="footer">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageInfo.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <!-- 添加菜品表单 -->
    <add-dish-form ref="addForm" @submit="handleAddDish" />
    <!-- 编辑菜品表单 -->
    <edit-dish-form ref="editForm" @submit="handleEditDish" />
  </div>
</template>

<script>
import { getDishes, addDish, updateDish, deleteDish, batchUpdateDishStatus } from "@/api/dish";
import AddDishForm from './AddDishForm.vue';
import EditDishForm from './EditDishForm.vue';

export default {
  components: {
    AddDishForm,
    EditDishForm,
  },
  data() {
    return {
      searchKeyword: "",
      tableData: [],
      pageInfo: { page: 1, pageSize: 10 },
      total: 0,
      selectedRows: [], // 选中的行
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      getDishes({ ...this.pageInfo, name: this.searchKeyword }).then((res) => {
        this.tableData = res.data.records;
        this.total = res.data.total;
      });
    },
    handleSizeChange(val) {
      this.pageInfo.page = 1;
      this.pageInfo.pageSize = val;
      this.init();
    },
    handleCurrentChange(val) {
      this.pageInfo.page = val;
      this.init();
    },
    openAddForm() {
      this.$refs.addForm.open();
    },
    openEditForm(row) {
      this.$refs.editForm.open(row);
    },
    handleSelectionChange(rows) {
      this.selectedRows = rows; // 更新选中的行
    },
    async handleAddDish(form) {
      try {
        const res = await addDish(form);
        if (res.code === 200) {
          this.$message.success("添加菜品成功");
          this.init();
        }
      } catch (error) {
        this.$message.error("添加菜品失败");
      }
    },
    async handleEditDish(form) {
      try {
        const res = await updateDish(form);
        if (res.code === 200) {
          this.$message.success("编辑菜品成功");
          this.init();
        }
      } catch (error) {
        this.$message.error("编辑菜品失败");
      }
    },
    async handleDelete(row) {
      try {
        await deleteDish([row.id]); // 删除单个菜品
        this.$message.success("删除菜品成功");
        this.init();
      } catch (error) {
        this.$message.error("删除菜品失败");
      }
    },
    async handleBatchDelete() {
      if (this.selectedRows.length === 0) {
        this.$message.warning("请选择要删除的菜品");
        return;
      }
      try {
        const ids = this.selectedRows.map((row) => row.id);
        await deleteDish(ids); // 批量删除
        this.$message.success("批量删除成功");
        this.init();
      } catch (error) {
        this.$message.error("批量删除失败");
      }
    },
    async handleBatchEnable() {
      if (this.selectedRows.length === 0) {
        this.$message.warning("请选择要启售的菜品");
        return;
      }
      try {
        const ids = this.selectedRows.map((row) => row.id);
        await batchUpdateDishStatus(ids, 1); // 批量启售
        this.$message.success("批量启售成功");
        this.init();
      } catch (error) {
        this.$message.error("批量启售失败");
      }
    },
    async handleBatchDisable() {
      if (this.selectedRows.length === 0) {
        this.$message.warning("请选择要停售的菜品");
        return;
      }
      try {
        const ids = this.selectedRows.map((row) => row.id);
        await batchUpdateDishStatus(ids, 0); // 批量停售
        this.$message.success("批量停售成功");
        this.init();
      } catch (error) {
        this.$message.error("批量停售失败");
      }
    },
    async handleChangeStatus(row) {
      try {
        await batchUpdateDishStatus([row.id], +!row.status); // 修改单个菜品状态
        this.$message.success("状态调整成功");
        this.init();
      } catch (error) {
        this.$message.error("状态调整失败");
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  padding: 33px;
  .header {
    display: flex;
    justify-content: space-between;
    .el-input {
      width: 250px;
      i {
        cursor: pointer;
      }
    }
  }
  .el-table {
    margin-top: 22px;
  }
  .footer {
    margin-top: 30px;
    display: flex;
    justify-content: center;
  }
}
</style>